package com.zhudi.develop_demo_as.ui.activity;

import android.content.Context;
import android.net.TrafficStats;
import android.os.Bundle;
import android.widget.TextView;

import com.zhudi.bean.DateTraffic;
import com.zhudi.develop.util.ZhudiFileUtil;
import com.zhudi.develop_demo_as.BaseActivity;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.db.FlowUtil;

import java.util.List;

import butterknife.Bind;

public class FlowActivity extends BaseActivity {
    private Context context;
    @Bind(R.id.tvDayFlow)
    TextView tvDayFlow;
    @Bind(R.id.tvWifiFlow)
    TextView tvWifiFlow;
    @Bind(R.id.tvUidFlow)
    TextView tvUidFlow;
    @Override
    protected int getLayoutView() {
        return R.layout.activity_flow;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();
        initDataFlow();
    }

    private void initDataFlow() {
        List<DateTraffic> toDayResult = FlowUtil.getInstance(context).getTypeFlow(0);
        //gps流量
        long mobileData = TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes();  // 通过Mobile连接收到的总字节数 +  通过Mobile发送的总字节数
        if (toDayResult != null && toDayResult.size() != 0)
             tvDayFlow.setText("软件3G流量"  + ZhudiFileUtil.FormetFileSize(toDayResult.get(0).getTraffic()));
        //总流量 - gps流量 = wifi流量
        long wifiData = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() - mobileData; //获取总的接受字节数，包含Mobile和WiFi等 +  总的发送字节数，包含Mobile和WiFi等  - mobile总字节数
        List<DateTraffic> toDayWifiResult = FlowUtil.getInstance(context).getTypeFlow(1);
        if (toDayWifiResult != null && toDayWifiResult.size() != 0)
             tvWifiFlow.setText("软件WIFI总流量" + ZhudiFileUtil.FormetFileSize(toDayWifiResult.get(0).getTraffic()));

        long received = TrafficStats.getUidRxBytes(android.os.Process.myUid());
        long transmitted = TrafficStats.getUidTxBytes(android.os.Process.myUid());
        tvUidFlow.setText("Uid总流量" + ZhudiFileUtil.FormetFileSize(received + transmitted));
        //long total = received + transmitted;
//        PackageManager packageManager = context.getPackageManager();
//        Intent intent = new Intent();
//        intent.setAction("android.intent.action.MAIN");
//        intent.addCategory("android.intent.category.LAUNCHER");
//        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
//        for (ResolveInfo resolveInfo : resolveInfos) {
//            String packageName = resolveInfo.activityInfo.packageName;
//            String name = resolveInfo.loadLabel(packageManager).toString();
//            int uid;
//            try {
//                PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
//                uid = packageInfo.applicationInfo.uid;
//                //long traffic = DbManager.getInstance(ctx).getTraffic(uid, DbManager.TrafficDate.MONTH, 0);
//                if (traffic == 0)
//                    continue;
//                AppModel model = new AppModel();
//                model.setTraffic(traffic);
//                model.setPkgName(packageName);
//                model.setUid(uid);
//                model.setAppName(name);
//                datas.add(model);
//            } catch (PackageManager.NameNotFoundException e) {
//                e.printStackTrace();
//            }
//        }
//        Collections.sort(datas, new Comparator<AppModel>() {
//            @Override
//            public int compare(AppModel lhs, AppModel rhs) {
//                return (int) (rhs.getTraffic() - lhs.getTraffic());
//            }
//        });
    }

    private void initView() {

    }

}
